'''# Third_party'''
from selenium import webdriver

'''# built_in'''
import os

'''# custom'''
from common import GSDSTORE
from reference import GSTORE
from action.page_Action import KeyWordTest


class EnvironmentConfiguration:

    def __repr__(self) -> str:
        return "测试环境配置"

    def __init__(self) -> None:
        '''浏览器预配置'''
        self.__prefs = {
            'profile.default_content_settings.popups': 0, 
            'download.default_directory': f'{GSDSTORE["WORKPATH"]["ROOT"]}\\download',
        }
        GSDSTORE["WORKPATH"]["downloadPath"] = f'{GSDSTORE["WORKPATH"]["ROOT"]}\\download'

    def __call__(self, browser=None, url=None, implicitly=None):
        '''Edge浏览器暂不支持“启动参数设置”'''
        options = webdriver.ChromeOptions() if browser == 'Chrome' else webdriver.IeOptions() if browser == 'IE' else webdriver.FirefoxOptions()
        options.add_experimental_option('prefs', self.__prefs)

        if browser == 'Chrome':
            driver = webdriver.Chrome(options = options)
        elif browser == 'IE':
            driver = webdriver.Ie(options = options)
        elif browser == 'Edge':
            driver = webdriver.Edge()
        else:
            '''Firefox'''
            self.driver = webdriver.Firefox(options = options, firefox_profile = options)

        driver.get(url)  # 打开被测系统
        driver.implicitly_wait(int(implicitly))  # 隐式等待
        driver.maximize_window()  # 窗口最大化

        GSTORE['driver'], GSDSTORE['driver'] = [driver, driver]
        keyWord = KeyWordTest(); GSTORE['keyWord'], GSDSTORE['keyWord'] = [keyWord, keyWord]